package com.example.springmvcdemo2;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Q
 * @create 2025-05-22-19:25
 */
@RequestMapping("/request")
@RestController
public class UserController {
    @RequestMapping("/param1")
    public String param1(String[] arrParams) {
        System.out.println("接收到的参数arrParams: " + Arrays.toString(arrParams));
        return "接收到的参数arrParams: " + Arrays.toString(arrParams);
    }
    @RequestMapping("/param2")
    public String param2(@RequestParam("listParam")List<String> listParam) {
        System.out.println("接收到的参数listParam: " + listParam);
        return "接收到的参数listParam: " + listParam;
    }
    @RequestMapping("/param3")
    public String param3(@RequestBody UserInfo userInfo) {
        System.out.println("接收到的参数userInfo: " + userInfo);
        return "接收到的参数userInfo: " + userInfo;
    }
    //获取一个参数
    @RequestMapping("/param4/{variable}")
    public String param4(@PathVariable String variable) {
        System.out.println("接收到的参数variable: " + variable);
        return "接收到的参数variable: " + variable;
    }
    //获取多个参数
    @RequestMapping("/param5/{name}/{id}")
    public String param5(@PathVariable String name,@PathVariable Integer id) {
        System.out.println("接收到的参数name: " + name + "id:" + id);
        return "接收到的参数name: " + name + "  id:" + id;
    }
    //上传文件
    @RequestMapping("/param6")
    public String param6(MultipartFile file) throws IOException {
        //获取文件名称
        String fileName = file.getOriginalFilename();
        //文件上传到指定目录
        file.transferTo(new File("D:\\test\\" + file.getOriginalFilename()));
        System.out.println("接收到的文件: " + fileName);
        return "接收到的文件: " + fileName;
    }
}
